<?php

/**
 *  管理 user 擁有 多少種類 及 多少數量 的 ServiceManager
 */
class ServiceManager
{
    protected $_services = Array();
    protected $_sessionManager = null;

    CONST SERVICE_ALL           = -1;
    CONST SERVICE_BLOG          = 'blog';
    CONST SERVICE_GALLERY_ALBUM = 'galleryAlbum';
    CONST SERVICE_CART          = 'cart';

    /**
     *  init
     */
    public function __construct()
    {
    }

    /**
     *  設定 SessionManager
     */
    public function setSessionManager( $sessionManager )
    {
        if( !$sessionManager OR !is_object($sessionManager) ) {
            return false;
        }
        $this->_sessionManager = $sessionManager;
    }

    // ====================================================================================================

    /**
     *  讀取 database (注意cache) 重新設定 service type and id
     */
    public function resetUserServicesByDatabase( $userId )
    {
        // blog
        $blogs = new Blogs();
        $myBlogs = $blogs->getBlogsByUserId( $userId );

        // focus first blog
        // ????
        
        if( $myBlogs ) {
            foreach( $myBlogs as $myBlog ) {
                $this->_addService( ServiceManager::SERVICE_BLOG, $myBlog->getId() );
            }
        }

    }

    /**
     *  設定 Service
     */
    protected function _addService( $type=null, $id )
    {
        switch( $type ) {
            case ServiceManager::SERVICE_BLOG :
                $this->_services[ ServiceManager::SERVICE_BLOG ][] = Array(
                    'id' => $id,
                );
                return true;
                break;
            case ServiceManager::SERVICE_GALLERY_ALBUM:
                $this->_services[ ServiceManager::SERVICE_GALLERY_ALBUM ][] = Array(
                    'id' => $id,
                );
                return true;
                break;
        }
        return false;
    }

    /**
     *  將設定好的 Service 儲存到 session 之中
     *  @return boolean;
     */
    public function saveToSession()
    {
        if( !$this->_sessionManager ) {
            return false;
        }
        if( !$this->_services ) {
            return false;
        }

        $this->_sessionManager->setValue( 'services', $this->_services );
        return true;
    }

    /**
     *  clear session about services info
     *  @return boolean;
     */
    public function clearToSession()
    {
        if( !$this->_sessionManager ) {
            return false;
        }
        $this->_sessionManager->setValue( 'services', Array() );
        return true;
    }

    // ====================================================================================================

    /**
     *  get focus blog by session
     *  @return blog object or false
     */
    public function getFocusBlog() 
    {
        $services = $this->_sessionManager->getValue('services');
        $key = ServiceManager::SERVICE_BLOG;

        if( $services && $services[$key] && $services[$key][0] ) {
            
            $blogId = $services[$key][0];
            $blogs = new Blogs();  
            if( $blogId && $blog = $blogs->getBlog($blogId) ) {
                return $blog;
            }

        }
        return false;
    }


}
